﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GBMS.APP_UI.UserControls;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GHRMS.BLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Project : System.Web.UI.Page
    {
        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        private EmployeeBLL empBLL = new EmployeeBLL();
        private SystemControlBLL scBLL = new SystemControlBLL();
        private OptionTypeBLL optionTypeBLL = new OptionTypeBLL();
        private int year;
        private string jobSiteID, type;
        private SourceBLL source = new SourceBLL();
        private RequestPass requestPass = new RequestPass();
        private Main main = new Main();
        private SsMng ssMng = new SsMng();

        public int BgmYear
        {
            get { return year; }
            set { year = value; }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {


            try
            {
                year = int.Parse(Request.QueryString["year"]);
            }
            catch
            {
                year = SystemInfo._CurrentYearReq;
            }

            if (Session["ProjectID"] == null)
            {
                Session["ProjectID"] = Request.QueryString["seq"];
            }

            try
            {
                projectBLL = Project_Factory.GetProjectFactory(type);
                int nBgmReqSeq = int.Parse(Request.QueryString["seq"]);
                LoadExecPos();
                LoadDataGrid();
                LoadMasterData();
                Temp(nBgmReqSeq.ToString());
                return;
            }
            catch
            {

            }

            type = Request.QueryString["type"].ToString();
            lbBgmYear.Text = year.ToString();
            if (type.Length > 0)
            {
                projectBLL = Project_Factory.GetProjectFactory(type);
                hdTypeID.Value = type;
                if (type == "rq")
                {
                    jobSiteID = Request.QueryString["id"].ToString();
                    Session["ProjectJobSiteSeq"] = jobSiteID;
                    
                    hdJobSiteID.Value = jobSiteID;
                    LoadJobSiteEmpHeadInfo(jobSiteID);
                }
                else if (type == "ap")
                {
                    lblProjectID.Text = Request.QueryString["id"].ToString();
                    Session["ProjectID"] = lblProjectID.Text;
                    GetProjectInfo();
                    gridSource.Columns[0].Visible = true;
                    gridSource.Columns[4].Visible = (type == "ap");
                    panelHeader.Visible = !(type == "ap");
                }
                else if (!Page.IsPostBack)
                {
                    Session["Approve"] = false;
                    LoadExecPos();
                    LoadDataGrid();
                    LoadMasterData();
                    SetVisibleControl("view");
                }
            }
        }

        private void Temp(string strBgmReqSeq)
        {
            string bgm_proj_seq = strBgmReqSeq;

            btnAddNew.Visible = false;
            btnDelete.Visible = false;
            btnApprove.Visible = false;
            btnEdit.Visible = false;

            ClearValueControl();
            lblProjectID.Text = strBgmReqSeq;
            Session["ProjectID"] = strBgmReqSeq;
            GetProjectInfo();
            gridSource.Columns[0].Visible = true;
            SetVisibleControl("info");
        }

        private void LoadShowAll()
        {
            string strJobSiteID = (string) new SsMng().GetUss(SsMng.SsUserType.WorkingJobSiteSeq);
            bool bStrategy = strJobSiteID == scBLL.JobSiteStrategyID();

            DataTable dtb = projectBLL.GetDataProjectByYearAndJobSite(year.ToString()
                                                                      , jobSiteID, "", "");
            RadGridShowAll.DataSource = dtb;
        }

        private void LoadExecPos()
        {
            DataRow dr = empBLL.ListEmployeeBySeq(ssMng.EmpSeq).Rows[0];
            lblExecPosID.Text = dr["exec_pos_seq"].ToString();
        }

        private void LoadDataGrid()
        {
            string strProjectSeq = lblProjectID.Text;
            gridSource.DataSource = projectBLL.GetDataProjectBySequenceForGridIn(strProjectSeq);
            gridSource.DataBind();

            gridManager.DataSource = projectBLL.GetDataProjectManager(strProjectSeq);
            gridManager.DataBind();

            LoadOption(strProjectSeq);

            repeaterJobSitePass.DataSource = requestPass.GetData(strProjectSeq);
            repeaterJobSitePass.DataBind();
        }

        private void LoadMasterData()
        {
            ddlStrategy.DataSource = comboBLL.ListStrategy(year.ToString());
            ddlStrategy.DataBind();

            ddlJobSite.DataSource = comboBLL.ListJobsite();
            ddlJobSite.DataBind();
            ddlJobSite.SelectedValue = jobSiteID;

            ddlProjType.DataSource = comboBLL.ListProjectTypeWithNewProjectComeFirst();
            ddlProjType.DataBind();

            ddlProjScope.DataSource = comboBLL.ListScope();
            ddlProjScope.DataBind();
            ddlProjScope.SelectedValue = "6";

            lbProjDate.Text = DateTime.Today.ToString("dd MMMM yyyy", new CultureInfo("th-TH"));
        }

        private void LoadOption(string strProjectSeq)
        {
            IList<OptionTypeInfo> optionTypeInfoList = optionTypeBLL.Search(-1);

            foreach (OptionTypeInfo info in optionTypeInfoList)
            {
                string strBgmOptionTypeSeq = info.Sequence.ToString();

                BGM_ProjectOption userControl =
                    (BGM_ProjectOption) Page.LoadControl("~/APP_UI/UserControls/BGM_ProjectOption.ascx");
                userControl.LoadUserControlData(strBgmOptionTypeSeq, strProjectSeq, type);

                PanelOption.Controls.Add(userControl);
            }
        }

        private void ClearValueControl()
        {
            lblProjectID.Text = "";

            lbProjName.Text = "-";
            lbProjStrat.Text = "-";

            if (hdJobSiteID.Value.Length > 0)
            {
                lbProjJobSite.Text = comboBLL.JobsiteInfo(Convert.ToInt32(hdJobSiteID.Value)).Rows[0]["job_site_name"].ToString();
            }

            lbProjType.Text = "-";
            lbProjScope.Text = "-";
            lbProjReason.Text = "-";
            lbProjObject.Text = "-";
            lbProjTarget.Text = "-";
            lbProjPlan.Text = "-";
            lbProjRemark.Text = "-";
            lblStartDate.Text = "-";
            lblEndDate.Text = "-";

            txtProjDate.Text = "";
            txtProjName.Text = "";
            ddlStrategy.ClearSelection();
            ddlStrategy.Text = string.Empty;

            ddlProjType.ClearSelection();
            ddlProjType.Text = string.Empty;
            ddlProjScope.ClearSelection();
            ddlProjScope.Text = string.Empty;

            txtProjReason.Text = "";
            txtProjObject.Text = "";
            txtProjTarget.Text = "";
            txtProjPlan.Text = "";
            txtProjRemark.Text = "";
            txtStartDate.SetValue("");
            txtEndDate.SetValue("");
        }

        private void SetVisibleControl(string mode)
        {
            bool bView = (mode == "view");
            bool bl = (mode == "add" || mode == "edit");
            bool bVisble = lblProjectID.Text.Length > 0;

            panelShowAll.Visible = bView;
            panel_Item1.Visible = !bView;
            btnApprove.Visible = !bView;

            btnCancel.Visible = (!bView);
            if (!bView)
            {
                lbPrefix.Visible = mode == "add";
                lbProjName.Visible = !bl;
                lbProjStrat.Visible = !bl;
                lbProjJobSite.Visible = true;
                lbProjJobSiteMain.Visible = !bl;

                lbProjType.Visible = !bl;
                lbProjScope.Visible = !bl;

                lbProjReason.Visible = !bl;
                lbProjObject.Visible = !bl;
                lbProjTarget.Visible = !bl;
                lbProjPlan.Visible = !bl;
                lbProjRemark.Visible = !bl;
                lblStartDate.Visible = !bl;
                lblEndDate.Visible = !bl;

                txtProjName.Visible = bl;
                ddlStrategy.Visible = bl;
                ddlJobSite.Visible = bl;

                ddlProjType.Visible = bl;
                ddlProjScope.Visible = bl;

                txtProjReason.Visible = bl;
                txtProjObject.Visible = bl;
                txtProjTarget.Visible = bl;
                txtProjPlan.Visible = bl;
                txtProjRemark.Visible = bl;
                txtStartDate.Visible = bl;
                txtEndDate.Visible = bl;
            }
            else
            {
                lbPrefix.Visible = false;
            }

            btnExpPlan.Enabled = bVisble;
            btnKPI.Enabled = bVisble;
            btnWorkPlan.Enabled = bVisble;
            btnManager.Enabled = bVisble;
            PanelOption.Enabled = bVisble;
            btnJobSitePass.Enabled = bVisble;
            btnPrintPreview.Enabled = !bView;

            bool bOwnProject = ssMng.WorkingJobSiteSeq == SecondaryProjectJobSiteSeq ||
                               ssMng.WorkingJobSiteSeq == PrimaryProjectJobSiteSeq;
             

            btnEdit.Enabled = (lblProjectID.Text.Length > 0 && !(bool) Session["Approve"] && bOwnProject);
            btnDelete.Enabled = (lblProjectID.Text.Length > 0 && !(bool) Session["Approve"] && bOwnProject);
            btnApprove.Enabled = (lblProjectID.Text.Length > 0 && !(bool) Session["Approve"] && bOwnProject);
            btnApprove.Visible = btnApprove.Enabled;
            btnSaveAddNew.Visible = (mode == "add");
            btnSaveEdit.Visible = (mode == "edit");

            if (lblProjectID.Text.Length > 0)
            {
                RadAjaxManager1.ResponseScripts.Add("onClickLink(1)");
                Session["ProjectID"] = lblProjectID.Text;
            }
            else
            {
                RadAjaxManager1.ResponseScripts.Add("onClickLink(0)");
            }

            btnSearchOldProject.Visible = false;
        }

        private void GetProjectInfo()
        {
            Common.Main main = new Common.Main();

            try
            {
                DataTable dt = projectBLL.GetDataProjectBySequence(lblProjectID.Text);

                if (dt.Rows.Count > 0)
                {
                    lbBgmYear.Text = dt.Rows[0]["bgm_year"].ToString();
                    lbProjDate.Text = Convert.ToDateTime(dt.Rows[0]["bgm_prj_date"]).ToString("dd MMMM yyyy",
                                                                                              new CultureInfo("th-TH"));
                    lbProjName.Text = dt.Rows[0]["bgm_prj_name"].ToString();
                    lbProjStrat.Text = dt.Rows[0]["bgm_strat_list_name"].ToString();
                    lbProjJobSiteSeq.Text = dt.Rows[0]["job_site_seq"].ToString();
                    lbProjJobSite.Text = dt.Rows[0]["job_site_name"].ToString();
                    lbProjJobSiteMain.Text = dt.Rows[0]["job_site_name_main"].ToString();
                    lbProjType.Text = dt.Rows[0]["prj_type_name"].ToString();
                    lbProjScope.Text = dt.Rows[0]["bgm_scope_name"].ToString();

                    lbProjReason.Text = (dt.Rows[0]["bgm_prj_reason"].ToString().Length > 0)
                                            ? main.WebString(dt.Rows[0]["bgm_prj_reason"].ToString())
                                            : "-";
                    lbProjObject.Text = (dt.Rows[0]["bgm_prj_object"].ToString().Length > 0)
                                            ? main.WebString(dt.Rows[0]["bgm_prj_object"].ToString())
                                            : "-";
                    lbProjTarget.Text = (dt.Rows[0]["bgm_prj_target"].ToString().Length > 0)
                                            ? main.WebString(dt.Rows[0]["bgm_prj_target"].ToString())
                                            : "-";
                    lbProjPlan.Text = (dt.Rows[0]["bgm_prj_plan"].ToString().Length > 0)
                                          ? main.WebString(dt.Rows[0]["bgm_prj_plan"].ToString())
                                          : "-";
                    lbProjRemark.Text = (dt.Rows[0]["bgm_prj_remark"].ToString().Length > 0)
                                            ? main.WebString(dt.Rows[0]["bgm_prj_remark"].ToString())
                                            : "-";
                    lblStartDate.Text = Convert.ToDateTime(dt.Rows[0]["date_start"]).ToString("dd/MM/yyyy",
                                                                                              new CultureInfo("th-TH"));
                    lblEndDate.Text = Convert.ToDateTime(dt.Rows[0]["date_end"]).ToString("dd/MM/yyyy",
                                                                                          new CultureInfo("th-TH"));

                    lbBgmYear.Text = dt.Rows[0]["bgm_year"].ToString();
                    txtProjDate.Text = dt.Rows[0]["bgm_prj_date"].ToString();
                    txtProjName.Text = dt.Rows[0]["bgm_prj_name"].ToString();
                    ddlStrategy.SelectedValue = dt.Rows[0]["bgm_strat_list_seq"].ToString();
                    ddlStrategy.Text = dt.Rows[0]["bgm_strat_list_name"].ToString();
                    ddlJobSite.SelectedValue = dt.Rows[0]["job_site_seq_main"].ToString();
                    ddlJobSite.Text = dt.Rows[0]["job_site_name_main"].ToString();
                    ddlProjType.SelectedValue = dt.Rows[0]["prj_type_seq"].ToString();
                    ddlProjType.Text = dt.Rows[0]["prj_type_name"].ToString();
                    ddlProjScope.SelectedValue = dt.Rows[0]["bgm_prj_scope"].ToString();
                    ddlProjScope.Text = dt.Rows[0]["bgm_scope_name"].ToString();

                    txtProjReason.Text = dt.Rows[0]["bgm_prj_reason"].ToString();
                    txtProjObject.Text = dt.Rows[0]["bgm_prj_object"].ToString();
                    txtProjTarget.Text = dt.Rows[0]["bgm_prj_target"].ToString();
                    txtProjPlan.Text = dt.Rows[0]["bgm_prj_plan"].ToString();
                    txtProjRemark.Text = dt.Rows[0]["bgm_prj_remark"].ToString();
                    txtStartDate.SetValue(dt.Rows[0]["date_start"]);
                    txtEndDate.SetValue(dt.Rows[0]["date_end"]);
                    DataTable dtb = comboBLL.GetInfoFromRefSeq(lblProjectID.Text, "bgm_request");

                    if (dtb.Rows.Count > 0)
                    {
                        lblReplyRemark.Text = dtb.Rows[0]["remarks"].ToString();
                        PanelReplyRemarks.Visible = (lblReplyRemark.Text.Length > 0);
                    }
                    else
                    {
                        lblReplyRemark.Text = "";
                        PanelReplyRemarks.Visible = false;
                    }

                    LoadDataGrid();

                    Session["ProjectName"] = lbProjName.Text;

                    //============ เตือนไม่มีข้อมูล ===============
                    DataTable dtb2;
                    dtb2 = projectBLL.GetDataProjectActivityPlan(lblProjectID.Text);
                    lbWorkPlanEmpty.Visible = (dtb2.Rows.Count == 0);
                    dtb2 = projectBLL.GetDataProjectExpensePlan(lblProjectID.Text, null);
                    lbExpPlanEmpty.Visible = (dtb2.Rows.Count == 0);
                    dtb2 = projectBLL.GetDataProjectKPI(lblProjectID.Text);
                    lbKPIEmpty.Visible = (dtb2.Rows.Count == 0);
                    //=======================================
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        protected void btnSearchOldProject_Click(object sender, EventArgs e)
        {
        }

        protected void btnShowStrategy_Click(object sender, EventArgs e)
        {
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            gridSource.Columns[0].Visible = false;

            ClearValueControl();
            SetVisibleControl("add");
            btnApprove.Visible = false;
            lblApproved.Text = "";

            gridManager.DataSource = null;
            gridManager.DataBind();

            PanelOption.Controls.Clear();
            LoadOption("");
            gridSource.DataSource = null;
            gridSource.DataBind();
            repeaterJobSitePass.DataSource = null;
            repeaterJobSitePass.DataBind();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            gridSource.Columns[0].Visible = true;
            SetVisibleControl("edit");
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            projectBLL.DeleteProject(lblProjectID.Text, new SsMng().EmpSeq);
            lblProjectID.Text = "";
            ClearValueControl();
            LoadShowAll();
            RadGridShowAll.DataBind();
            SetVisibleControl("view");
        }

        protected void btnSaveAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtStartDate.GetStringValue().Length == 0)
                {
                    Label2.Visible = true;
                    return;
                }

                if (txtEndDate.GetStringValue().Length == 0)
                {
                    Label1.Visible = true;
                    return;
                }

                Label1.Visible = false;
                Label2.Visible = false;

                // บันทึกโครงการใหม่ พร้อมได้รับค่า ID โครงการใหม่ที่เพิ่งสร้าง
                int reqSeqTemp = projectBLL.InsertProject(lbBgmYear.Text, DateTime.Today
                                                          , "โครงการ" + txtProjName.Text, ddlStrategy.SelectedValue,
                                                          hdJobSiteID.Value
                                                          , ddlProjType.SelectedValue, ddlProjScope.SelectedValue,
                                                          txtProjReason.Text
                                                          , txtProjObject.Text, txtProjPlan.Text, txtProjTarget.Text,
                                                          txtProjAim.Text, txtProjProfit.Text,
                                                          txtProjRemark.Text
                                                          , txtStartDate.ValueS, txtEndDate.ValueS, ssMng.EmpSeq
                                                          , ddlJobSite.SelectedValue);
                string reqSeq = Convert.ToString(reqSeqTemp);

                // ใส่ผู้รับผิดชอบ#1 เป็นผู้บังคับบัญชาบนสุดตามสายบังคับบัญชา
                DataRow empHead = comboBLL.GetJobSiteHeadEmployee(jobSiteID);
                string commanderEmpSeq = "";
                if (empHead != null && (empHead["emp_seq"] as int?).HasValue)
                    commanderEmpSeq = Convert.ToString(empHead["emp_seq"]);
                projectBLL.InsertProjectManager(reqSeq, commanderEmpSeq);

                // ใส่ผู้รับผิดชอบ#2 เป็นผู้ที่กำลังทำการสร้างโครงการ (กรณีผู้บังคับบัญชาไม่ได้เป็นคนสร้างเอง)
                if (ssMng.EmpSeq != commanderEmpSeq)
                    projectBLL.InsertProjectManager(reqSeq, ssMng.EmpSeq);

                lblProjectID.Text = reqSeq;

                Page.ClientScript.RegisterStartupScript(
                    GetType(), "Message", "alert('บันทึกเรียบร้อยแล้ว');", true);

            }
            catch (Exception error)
            {

                Page.ClientScript.RegisterStartupScript(
                    GetType(), "Message",
                    "alert('เกิดข้อผิดพลาดในการบันทึกข้อมูล\n" + main.EncodeHtml(error.Message) + "');", true);

            }

            LoadShowAll();
            RadGridShowAll.DataBind();
            GetProjectInfo();

            gridSource.Columns[0].Visible = true;

            SetVisibleControl("info");

        }

        protected void btnSaveEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Nullable<DateTime> dateS = txtStartDate.GetDateValue();
                Nullable<DateTime> dateE = txtEndDate.GetDateValue();

                //projectBLL.UpdateProject(lblProjectID.Text
                    
                //    , lbBgmYear.Text, DateTime.Today
                //                         , txtProjName.Text, null, ddlJobSite.SelectedValue, ddlStrategy.SelectedValue, hdJobSiteID.Value
                //                         , ddlProjType.SelectedValue, ddlProjScope.SelectedValue, txtProjReason.Text
                //                         , txtProjObject.Text, txtProjPlan.Text, txtProjTarget.Text, txtProjRemark.Text
                //                         , dateS.ToString(), dateE.ToString(), ssMng.EmpSeq);
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('บันทึกเรียบร้อย');",
                                                             true);
            }
            catch
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message",
                                                             "alert('เกิดข้อผิดพลาดในการบันทึกข้อมูล');", true);
            }

            LoadShowAll();
            RadGridShowAll.DataBind();
            GetProjectInfo();

            gridSource.Columns[0].Visible = false;

            SetVisibleControl("info");
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            gridSource.Columns[0].Visible = false;
            lblApproved.Text = "";
            ClearValueControl();
            SetVisibleControl("view");
        }

        protected void Approve_Click(object sender, EventArgs e)
        {

        }


        protected void Approve2_Click(object sender, EventArgs e)
        {
            try
            {
                projectBLL.ApproveProject(lblProjectID.Text, "A");
                ClearValueControl();
                SetVisibleControl("info");
            }
            catch
            {
                RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึกข้อมูล");
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string strArg = e.Argument;
            string strProjectSeq = lblProjectID.Text;

            if (strArg == "refresh")
            {
                string strRecordStatus =
                    projectBLL.GetDataProjectBySequence(lblProjectID.Text).Rows[0]["record_status"].ToString();

                if (strRecordStatus == "P")
                {
                    lblApproved.Text = "รออนุมัติ";
                    btnApprove.Visible = false;
                    btnEdit.Enabled = false;
                }
            }
            else if (strArg == "JobSitePass")
            {
                repeaterJobSitePass.DataSource = requestPass.GetData(strProjectSeq);
                repeaterJobSitePass.DataBind();
            }
            else
            {
                LoadDataGrid();
            }
        }

        protected void gridSource_ItemDataBound(object sender, GridItemEventArgs e)
        {
        }

        protected void RadGridShowAll_EditCommand(object source, GridCommandEventArgs e)
        {
            string bgm_proj_seq = ((GridDataItem) e.Item)["Sequence"].Text;
            string record_status = ((GridDataItem) e.Item)["record_status"].Text;

            string ne = "ยังไม่ส่ง", pe = "รออนุมัติ", ap = "อนุมัติแล้ว", re = "ตีกลับ";
            bool Director = (lblExecPosID.Text == "234"
                             || lblExecPosID.Text == "235"
                             || lblExecPosID.Text == "243"
                             || lblExecPosID.Text == "251");
            Session["Approve"] = false;
            if (record_status == "W")
            {
                lblApproved.Text = "";
                btnApprove.Visible = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (record_status == "P" && (Director)) // ผอ.สำนัก
            {
                lblApproved.Text = "";
                btnApprove.Visible = false;
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                Session["Approve"] = true;
            }
            else if (record_status == "P")
            {
                lblApproved.Text = pe;
                btnApprove.Visible = false;
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                Session["Approve"] = true;
            }
            else if (record_status == "A")
            {
                lblApproved.Text = ap;
                btnApprove.Visible = false;
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                Session["Approve"] = true;
            }
            else if (record_status == "R")
            {
                lblApproved.Text = "";
                btnApprove.Visible = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }

            ClearValueControl();
            lblProjectID.Text = bgm_proj_seq;
            Session["ProjectID"] = bgm_proj_seq;
            GetProjectInfo();
            gridSource.Columns[0].Visible = true;
            SetVisibleControl("info");
        }

        protected void RadGridShowAll_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            LoadShowAll();
        }

        protected void cmdExportToMsWord_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void cmdExportToMsExcel_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ExportToMsWord(Control ctrl)
        {
            string strFileName = "Export-A.doc";
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-word";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            ctrl.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }

        protected void repeaterJobSitePass_ItemCommand(object source,
                                                       System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string strProjectSeq = lblProjectID.Text;
                Label lblBgmReqPassSeq = (Label) e.Item.FindControl("lblBgmReqPassSeq");
                requestPass.Delete(lblBgmReqPassSeq.Text, ssMng.EmpSeq);

                repeaterJobSitePass.DataSource = requestPass.GetData(strProjectSeq);
                repeaterJobSitePass.DataBind();
            }



        }

        protected void LoadJobSiteEmpHeadInfo(string selectedJobSiteSeq)
        {
            string errorMessage = "";
            DataRow dr = comboBLL.GetJobSiteHeadEmployee(selectedJobSiteSeq, ref errorMessage);
            lblJobSiteHeadPosLoadError.Text = errorMessage;
            if (String.IsNullOrEmpty(errorMessage))
                lblJobSiteHeadPos.Text = dr["fullnamepos"] as string;
        }

        private bool IsPlanDepartment
        {
            get{         
                return (jobSiteID == scBLL.JobSiteStrategyID() || jobSiteID == ""); // เช็คเป็นฝ่ายแผน หรือ เป็นผู้บริหารที่เหนือกว่าสำนักหรือไม่
            }
        }
        private string SecondaryProjectJobSiteSeq
        {
            get { return lbProjJobSiteSeq.Text; }
        }

        private string PrimaryProjectJobSiteSeq
        {
            get { return ddlJobSite.SelectedValue; }
        }

        private bool IsStateCreatingNewProject
        {
            get { return String.IsNullOrEmpty(SecondaryProjectJobSiteSeq); }
        }

        private bool CanApprove
        {
            get { return (bool) Session["Approve"]; }
        }

        private bool IsOwnProject
        {
            get
            {
                return ssMng.WorkingJobSiteSeq == SecondaryProjectJobSiteSeq ||
                       ssMng.WorkingJobSiteSeq == PrimaryProjectJobSiteSeq;
            }
        }

        private bool IsProjectManager
        {
            get { return CheckIsProjectManager(ssMng.EmpSeq); }
        }

        private bool CheckIsProjectManager(string empSeq)
        {
            DataTable projectManagerData = projectBLL.GetDataProjectManager(SecondaryProjectJobSiteSeq);

            foreach(DataRow each in projectManagerData.Rows)
            {
                if(empSeq == Convert.ToString(each["emp_seq"]))
                {
                    return true;
                }
            }

            return false;
        }

        private bool IsInPrimaryJobSite()
        {
            return jobSiteID == PrimaryProjectJobSiteSeq;
        }

        private bool IsInRequestPass
        {
            get 
            { 
                DataTable dataTable = requestPass.GetData(lblProjectID.Text);

                foreach (DataRow each in dataTable.Rows)
                {
                    if (ssMng.WorkingJobSiteSeq == Convert.ToString(each["job_site_seq"]))
                    {
                        return true;
                    }
                }

                return false;
            }
        }
    }
}
